package com.sumainfor.pc.market.service;

import com.qiniu.util.Json;
import com.sumainfor.common.entity.ParentDeptVo;
import com.sumainfor.common.entity.SysUserVo;
import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.PageUtils;
import com.sumainfor.pc.market.dao.RdJobMapper;
import com.sumainfor.pc.market.dao.SeWorderMapper;
import com.sumainfor.pc.market.feign.Zfeign.ZzuulFeign;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 服务中心经理 租赁顾问管理
 */
@Service
public class SeWorderService implements Serializable {

    private ReentrantLock lock = new ReentrantLock(false);

    Logger log = LoggerFactory.getLogger(SeWorderService.class);// 日志

    @Autowired
    private SeWorderMapper seWorderMapper;
    @Autowired
    private ZzuulFeign zzuulFeign;
    /**
     * 查询租赁顾问的列表
     */
    public JsonResult getSeWorderList(Map<String, Object> params){
        //获取当前登录人的服务中心编号
        SysUserVo userVo = ContextUtils.getSysUser();
        ParentDeptVo vo = null;
        for (int i = 0; i < userVo.getLength(); i++) {
            ParentDeptVo vo1 = userVo.getParentDeptVo();
            if (vo1.getType().equals(3)) {
                vo = vo1;
                break;
            }
        }
        params.put("role_id",3);//租赁顾问 ：3
        if (vo!=null){
            System.out.println("area_id:"+vo.getDeptId());
            params.put("SeId",vo.getDeptId());//当前登录人的服务中心编号
            params.put("area_id",vo.getDeptId());//当前登录人的服务中心编号
            System.out.println("area_id:"+vo.getDeptId());
        }
        JsonResult result=new JsonResult();
        PageUtils pageUtils = new PageUtils();
        List<Map<String,Object>> marketList=seWorderMapper.getSeWorderList(params);
        //查询网关的租赁顾问姓名
        JsonResult zuulMap = zzuulFeign.getWoUserIdList(params);
        List<Map<String, Object>> zuulList = (List<Map<String, Object>>) zuulMap.get("result");
        for (Map<String, Object> map : marketList) {
            if (map.get("TwfSetId")!=null){
                for (Map<String, Object> map2 : zuulList) {
                    if (map2.get("user_id")!=null) {
                        if (MapUtils.getString(map, "TwfSetId").equals(MapUtils.getString(map2, "user_id"))) {
                            map.putAll(map2);
                        }
                    }
                }
            }
        }
        return pageUtils.getJsonResult(marketList, params,marketList.size());
    }
    /**
     * 查询租赁顾问管理的成绩列表
     */
    public JsonResult getSeWorTenList(Map<String,Object> params){
        JsonResult result=new JsonResult();
        if (params.get("SetId")==null){
            return result.putFailed("参数不正确");
        }
        List<Map<String,Object>> list=seWorderMapper.getSeWorTenList(params);
        if (list!=null){
            result.put(list);
        }else{
            result.putFailed("服务器繁忙");
        }
        return result;
    }
    /**
     * 查询租赁顾问本月流失的客户列表
     */
    public JsonResult getSeWorTraList(Map<String,Object> params){
        JsonResult result=new JsonResult();
        if (params.get("SetId")==null){
            return result.putFailed("参数不正确");
        }
        List<Map<String,Object>> list=seWorderMapper.getSeWorTraList(params);
        if (list!=null){
            result.put(list);
        }else{
            result.putFailed("服务器繁忙");
        }
        return result;
    }
    /**
     * 查询服务中心下的工单列表数据统计
     */
    public JsonResult getSeWorderMidList(Map<String,Object> params){
        JsonResult result=new JsonResult();
        List<Map<String,Object>> list=seWorderMapper.getSeWorderMidList(params);
        //查询所有服务中心
        JsonResult zuulMap = zzuulFeign.getSysDeptInfoList();
        List<Map<String, Object>> zuulList = (List<Map<String, Object>>) zuulMap.get("result");
        if(list.size()>0) {
            for (Map<String, Object> map : list) {
                if(MapUtils.getString(map,"SeId")!=null){
                //if (map.get("SeId") != null) {
                    for (Map<String, Object> map2 : zuulList) {
                        if(MapUtils.getString(map2,"dept_id")!=null){
                      //  if (map2.get("dept_id") != null) {
                            if (MapUtils.getString(map, "SeId").equals(MapUtils.getString(map2, "dept_id"))) {
                                map.putAll(map2);
                            }
                        }
                    }
                }
            }
        }
        if (list!=null){
            result.put(list);
        }else{
            result.putFailed("服务器繁忙");
        }
        return  result;
    }
}
